嗨,我正在尝试使用硒单击此链接,但未获得点击
这是网站上的HTML
StringBuffer
这就是我在代码中尝试的方式
$marks = ['Spades', 'Hearts', 'Diamonds', 'Cloves'];
for ($mark = 0; $mark < 4; $mark++) {
for ($cards = 1; $cards < 14; $cards++) {
$decks[] = ['number' =>$cards, 'mark' => $marks[$mark]];
}
}
while(count($decks) > 0) {
$random_card_index = array_rand($decks);
$card1 = $decks[$random_card_index];
unset($decks[$random_card_index]);
$random_card_index = array_rand($decks);
$card2 = $decks[$random_card_index];
unset($decks[$random_card_index]);
while ($card1['number'] === $card2['number']) {
$player1[] = $card1;
$player1[] = $card2;
if (count($decks) < 1){
break;
}
$random_card_index = array_rand($decks);
$card1 = $decks[$random_card_index];
unset($decks[$random_card_index]);
$random_card_index = array_rand($decks);
$card2 = $decks[$random_card_index];
unset($decks[$random_card_index]);
}
if (count($decks) < 1){
break;
}
if ($card1['number'] !== $card2['number']) {
$decks[] = $card1;
$decks[] = $card2;
$random_card_index = array_rand($decks);
$card1 = $decks[$random_card_index];
unset($decks[$random_card_index]);
$random_card_index = array_rand($decks);
$card2 = $decks[$random_card_index];
unset($decks[$random_card_index]);
}
while ($card1['number'] === $card2['number']) {
$player2[] = $card1;
$player2[] = $card2;
if (count($decks) < 1){
break;
}
$random_card_index = array_rand($decks);
$card1 = $decks[$random_card_index];
unset($decks[$random_card_index]);
$random_card_index = array_rand($decks);
$card2 = $decks[$random_card_index];
unset($decks[$random_card_index]);
}
if ($card1['number'] !== $card2['number']) {
$decks[] = $card1;
$decks[] = $card2;
}
}
echo 'player1have'.count($player1).'pairs'. '<br>';
echo 'player2have'.count($player2).'pairs'. '<br>';
if (count($player1) > count($player2)){
echo 'player1won!!!'. '<br>';
} elseif (count($player1) === count($player2)) {
echo 'Draw!!!'. '<br>';
} else {
echo 'player2won!!!'. '<br>';
}
能否请您检查代码并告诉我我做错了什么? 这是完整的代码。
<td class=" sorting_1" data-key="title" data-label="Title"><a class="document-link" href="/view/html/inforce/current/act-1984-051?query=((Repealed%3DN+AND+PrintType%3D%22act.reprint%22+AND+PitValid%3D%40pointInTime(20190411000000))+OR+(Repealed%3DN+AND+PrintType%3D%22reprint%22+AND+PitValid%3D%40pointInTime(20190411000000)))+AND+Title%3D(%22Aboriginal+and+Torres+Strait+Islander+Communities+(Justice%2C+Land+and+Other+Matters)+Act+1984%22)&dQuery=Document+Types%3D%22%3Cspan+class%3D'dq-highlight'%3EActs%3C%2Fspan%3E%2C+%3Cspan+class%3D'dq-highlight'%3ESL%3C%2Fspan%3E%22%2C+Search+In%3D%22%3Cspan+class%3D'dq-highlight'%3ETitle%3C%2Fspan%3E%22%2C+Exact+Phrase%3D%22%3Cspan+class%3D'dq-highlight'%3EAboriginal+and+Torres+Strait+Islander+Communities+(Justice%2C+Land+and+Other+Matters)+Act+1984%3C%2Fspan%3E%22%2C+Point+In+Time%3D%22%3Cspan+class%3D'dq-highlight'%3E11%2F04%2F2019%3C%2Fspan%3E%22">Aboriginal and Torres Strait Islander Communities (Justice, Land and Other Matters) Act 1984</a></td>
我希望点击标签href链接并移至下一页。
答案 0 :(得分:1)
要在文本为{strong> 1984年《土著和托雷斯海峡岛民社区(司法,土地和其他事项)法》 的元素上click()
,您需要引入 WebDriverWait 为使元素可点击,您可以使用以下任一Locator Strategies:
使用LINK_TEXT
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Aboriginal and Torres Strait Islander Communities (Justice, Land and Other Matters) Act 1984"))).click()
使用PARTIAL_LINK_TEXT
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Aboriginal and Torres Strait Islander Communities (Justice, Land and Other Matters) Act 1984"))).click()
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td.sorting_1[data-key='title'][data-label='Title']>a.document-link[href^='/view/html/inforce/current/act-1984-051']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[@class=' sorting_1' and @data-key='title'][@data-label='Title']/a[@class='document-link' and starts-with(@href, '/view/html/inforce/current/act-1984-051')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
答案 1 :(得分:0)
从您的Xpath删除[@href]
,您需要一个到webElement的Xpath,而不是到webElement的属性。